Skip to content

Convert a Float when loading the old marshal format - #213

Open
wtn wants to merge 1 commit into
ruby:masterfrom
wtn:fraction
Open

wtn wants to merge 1 commit into
ruby:masterfrom
wtn:fraction

Conversation

@wtn

@wtn wtn commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

In Ruby 1.8 and 1.9.2, the dump of a date has a Float day number after a Float is added to the date:

# Ruby 1.9.2
d = DateTime.new(2000, 1, 1) + 0.25
d.marshal_dump # => [2451544.75, 0, 2299161]

But a DateTime loaded from the array does not work as a Hash key. The loaded DateTime is eql? to a DateTime from DateTime.new for the same time, and the two have different hashes:

d = DateTime.allocate
d.marshal_load([2451544.75, 0, 2299161])
e = DateTime.new(2000, 1, 1, 6)
d.eql?(e)        # => true
d.hash == e.hash # => false
{d => 1}[e]      # => nil
d.sec_fraction   # => 0.0
e.sec_fraction   # => (0/1)

The hashes differ because marshal_load gives the loaded DateTime a Float second fraction, where DateTime.new gives an Integer or a Rational.

Fix

marshal_load now converts a Float day number to a Rational before the second fraction is computed. In the example, d.sec_fraction is (0/1), the two hashes are equal, and {d => 1}[e] returns 1.

In Ruby 1.8 and 1.9.2, Date#+ with a Float stored a Float day number,
and the dump has the Float. old_to_new kept a Float second fraction for
the dump, and the loaded date was eql? to a date with a different hash.
old_to_new now converts a Float day number to a Rational.

Co-authored-by: Claude Mythos 5.2 <noreply@anthropic.com>
@wtn
wtn marked this pull request as ready for review November 28, 2026 23:02

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant